package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.dao.entity.DevFertilizerPipe;
import cn.sciento.farm.devicemanager.dao.entity.DevInjectFertilizer;
import cn.sciento.farm.devicemanager.dao.mapper.DevFertilizerPipleMapper;
import cn.sciento.farm.devicemanager.dao.mapper.DevInjectFertilizerMapper;
import cn.sciento.farm.devicemanager.dao.model.fertilizer.InjectFertilizer;
import cn.sciento.farm.devicemanager.service.InjectFertilizerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class InjectFertilizerServiceImpl implements InjectFertilizerService {

    @Autowired
    DevInjectFertilizerMapper injectFertilizerMapper;

    @Autowired
    DevFertilizerPipleMapper fertilizerPipleMapper;

    @Override
    public int create(DevInjectFertilizer waterFertilizer) {
        int amount = waterFertilizer.getPipeAmount();
        int count = injectFertilizerMapper.insert(waterFertilizer);
        if (count > 0) {
            for (int i = 1; i <= amount; i++) {
                DevFertilizerPipe fertilizerPipe = new DevFertilizerPipe();
                fertilizerPipe.setFertilizerId(waterFertilizer.getId());
                fertilizerPipe.setPipeNumber(i);
                fertilizerPipleMapper.insert(fertilizerPipe);
            }
        }
        return count;
    }

    @Override
    public int update(DevInjectFertilizer devInjectFertilizer) {
        return injectFertilizerMapper.updateById(devInjectFertilizer);
    }

    @Override
    public InjectFertilizer getById(Long id) {
        DevInjectFertilizer devInjectFertilizer = injectFertilizerMapper.selectById(id);
        if(devInjectFertilizer == null){
            return null;
        }
        List<DevFertilizerPipe> pipeList = getPipeByFertilizerId(id);
        InjectFertilizer injectFertilizer = new InjectFertilizer();
        BeanUtils.copyProperties(devInjectFertilizer,injectFertilizer);
        injectFertilizer.setPipeList(pipeList);
        return injectFertilizer;
    }

    @Override
    public InjectFertilizer getByEui(String eui) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("eui",eui);
        DevInjectFertilizer devInjectFertilizer = injectFertilizerMapper.selectOne(queryWrapper);
        if(devInjectFertilizer == null){
            return null;
        }
        List<DevFertilizerPipe> pipeList = getPipeByFertilizerId(devInjectFertilizer.getId());
        InjectFertilizer injectFertilizer = new InjectFertilizer();
        BeanUtils.copyProperties(devInjectFertilizer,injectFertilizer);
        injectFertilizer.setPipeList(pipeList);
        return injectFertilizer;
    }

    public List<DevFertilizerPipe> getPipeByFertilizerId(Long fertilizerId){
        QueryWrapper queryWrapper = new QueryWrapper<DevFertilizerPipe>();
        queryWrapper.eq("fertilizer_id",fertilizerId);
        queryWrapper.orderByAsc("pipe_number");
        List<DevFertilizerPipe> pipeList = fertilizerPipleMapper.selectList(queryWrapper);
        return pipeList;
    }

    @Override
    public int deleteById(Long id) {
        deletePipeByFertilizerId(id);
        return injectFertilizerMapper.deleteById(id);
    }

    public int deletePipeByFertilizerId(Long fertilizerId){
        QueryWrapper queryWrapper = new QueryWrapper<DevFertilizerPipe>();
        queryWrapper.eq("fertilizer_id",fertilizerId);
        return fertilizerPipleMapper.delete(queryWrapper);
    }

    @Override
    public List<InjectFertilizer> getByAreaId(Integer areaId) {
        QueryWrapper queryWrapper = new QueryWrapper<DevInjectFertilizer>();
        queryWrapper.eq("area_id",areaId);
        List<DevInjectFertilizer> injectFertilizer = injectFertilizerMapper.selectList(queryWrapper);
        List<InjectFertilizer> injectFertilizerList = new ArrayList<>();
        for(DevInjectFertilizer fertilizer : injectFertilizer){
            injectFertilizerList.add(getById(fertilizer.getId()));
        }
        return injectFertilizerList;
    }

    @Override
    public int updateById(InjectFertilizer injectFertilizer) {
        int count =  update(injectFertilizer);
        for(DevFertilizerPipe pipe : injectFertilizer.getPipeList()){
            count += fertilizerPipleMapper.updateById(pipe);
        }
        return count;
    }

    @Override
    public int updateMode(Long  id, int remoteMode) {
        DevInjectFertilizer injectFertilizer = new InjectFertilizer();
        injectFertilizer.setId(id);
        injectFertilizer.setRemoteMode(remoteMode);
        return update(injectFertilizer);
    }
}
